package com.xjoyt.pay.operater.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.operater.auth.po.CompanyNode;
import com.xjoyt.pay.operater.repository.CompanyNodeRepository;

@Service
public class CompanyService {

	@Autowired
	private CompanyNodeRepository companyNodeRepository;

	public ExecuteResult<?> saveNode(String id, List<String> nodeList) {
		companyNodeRepository.findAllByCompanyId(id).forEach(item -> companyNodeRepository.delete(item.id));
		for (String item : nodeList) {
			CompanyNode entity = new CompanyNode();
			entity.nodeId = item;
			entity.companyId = id;
			companyNodeRepository.save(entity);
		}
		return ExecuteResult.ok();
	}

	public List<String> getNodeList(String id) {
		List<String> list = new ArrayList<>();
		companyNodeRepository.findAllByCompanyId(id).forEach(item -> list.add(item.nodeId));
		return list;
	}
}
